package com.titar.robot.http.interceptor;

import android.content.Context;
import android.content.SharedPreferences;

import com.titar.robot.utils.SharedPreUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


public class BasicAuthInterceptor implements Interceptor {
    private Context context;

    public BasicAuthInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Interceptor.Chain chain) {
        Request request = chain.request();
        String accessToken = getAuthorization();

        if (!accessToken.isEmpty()) {
            String url = request.url().toString();
            request = request.newBuilder()
                    //.addHeader("token", accessToken)
                    .url(url)
                    .build();
        }

        try {
            return chain.proceed(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getAuthorization() {
        String accessToken = SharedPreUtils.getInstence(context).getToken();
        return accessToken;
    }
}